﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace movie.tool.library.controls
{
    public class Pages
    {
        public string Name { get; set; }
        public int PageIndex { get; set; }
    }
    /// <summary>
    /// Paging.xaml 的交互逻辑
    /// </summary>
    public partial class Paging : UserControl
    {
        public static readonly DependencyProperty SizeProperty = DependencyProperty.Register("Size", typeof(int), typeof(Paging),new PropertyMetadata(10));
        public static readonly DependencyProperty CurrentProperty = DependencyProperty.Register("Current",typeof(int),typeof(Paging),new PropertyMetadata(1));
        public static readonly DependencyProperty TotalProperty = DependencyProperty.Register("Total", typeof(int), typeof(Paging));
        public static readonly DependencyProperty PagesProperty = DependencyProperty.Register("Pages",typeof(int),typeof(Paging),new PropertyMetadata(1));
        public static readonly DependencyProperty PreVisibleProperty = DependencyProperty.Register("PreVisible",typeof(Visibility),typeof(Paging),new PropertyMetadata(Visibility.Collapsed));
        public static readonly DependencyProperty NextVisibleProperty = DependencyProperty.Register("NextVisible", typeof(Visibility), typeof(Paging),new PropertyMetadata(Visibility.Collapsed));
        public static readonly DependencyProperty LastVisibleProperty = DependencyProperty.Register("LastVisible", typeof(Visibility), typeof(Paging), new PropertyMetadata(Visibility.Collapsed));
        public static readonly DependencyProperty PageCollectionProperty = DependencyProperty.Register("PageCollection",typeof(ObservableCollection <Pages>),typeof(Paging));
        public static readonly DependencyProperty GotoPageProperty = DependencyProperty.Register("GotoPage", typeof(int), typeof(Paging),new PropertyMetadata(1));
        public static readonly RoutedEvent LoadDataEvent = EventManager.RegisterRoutedEvent("LoadData", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Paging));
        /// <summary>
        /// 分页大小
        /// </summary>
        public int Size
        {
            get => (int)GetValue(SizeProperty);
            set => SetValue(SizeProperty, value);
        }
        /// <summary>
        /// 当前页号
        /// </summary>
        public int Current
        {
            get => (int)GetValue(CurrentProperty);
            set => SetValue(CurrentProperty, value);
        }
        /// <summary>
        /// 总记录数
        /// </summary>
        public int Total
        {
            get => (int)GetValue(TotalProperty);
            set => SetValue(TotalProperty, value);
        }
        /// <summary>
        /// 总页数
        /// </summary>
        public int Pages
        {
            get => (int)GetValue(PagesProperty);
            set => SetValue(PagesProperty, value);
        }
        public Visibility PreVisible
        {
            get => (Visibility)GetValue(PreVisibleProperty);
            set => SetValue(PreVisibleProperty, value);
        }
        public Visibility NextVisible
        {
            get => (Visibility)GetValue(NextVisibleProperty);
            set => SetValue(NextVisibleProperty, value);
        }
        public Visibility LastVisible
        {
            get => (Visibility)GetValue(LastVisibleProperty);
            set => SetValue(LastVisibleProperty, value);
        }
        public ObservableCollection<Pages> PageCollection
        {
            get => (ObservableCollection<Pages>)GetValue(PageCollectionProperty);
            set => SetValue(PageCollectionProperty, value);
        }
        public int GotoPage
        {
            get => (int)GetValue(GotoPageProperty);
            set => SetValue(GotoPageProperty, value);
        }
        public event RoutedEventHandler LoadData
        {
            add
            {
                AddHandler(LoadDataEvent, value);
            }
            remove
            {
                RemoveHandler(LoadDataEvent, value);
            }
        }
        public Paging()
        {
            InitializeComponent();
        }

        private void PrePageClick(object sender, RoutedEventArgs e)
        {
            if(Current > 1)
            {
                --Current;
                RaiseEvent(new RoutedEventArgs(LoadDataEvent,this));
            }
        }

        private void FirstPageClick(object sender, RoutedEventArgs e)
        {
            if(Current != 1)
            {
                Current = 1;
                RaiseEvent(new RoutedEventArgs(LoadDataEvent, this));
            }
        }

        private void PreDotClick(object sender, RoutedEventArgs e)
        {
            if (Current > 1)
            {
                int to = Current - 5;
                to = to < 1 ? 1 : to;
                Current = to;
                RaiseEvent(new RoutedEventArgs(LoadDataEvent, this));
            }
        }

        private void GotoPageClick(object sender, RoutedEventArgs e)
        {
            if(GotoPage >=1 && GotoPage <= Pages)
            {
                Current = GotoPage;
                RaiseEvent(new RoutedEventArgs(LoadDataEvent, this));
            }
        }

        private void NextDotClick(object sender, RoutedEventArgs e)
        {
            if (Current < Pages)
            {
                int to = Current + 5;
                to = to > Pages ? Pages : to;
                Current = to;
                RaiseEvent(new RoutedEventArgs(LoadDataEvent, this));
            }
        }

        private void LastPageClick(object sender, RoutedEventArgs e)
        {
            if(Current != Pages)
            {
                Current = Pages;
                RaiseEvent(new RoutedEventArgs(LoadDataEvent, this));
            }
        }

        private void NextPageClick(object sender, RoutedEventArgs e)
        {
            if(Current < Pages)
            {
                ++Current;
                RaiseEvent(new RoutedEventArgs(LoadDataEvent, this));
            }
        }
        /// <summary>
        /// 计算当前应该显示的页号的集合
        /// </summary>
        public void CalcPageCollection()
        {
            int maxAjust = 0;
            int minAjust = 0;
            int min = Current - 2;
            if(min <= 2)
            {
                maxAjust = 2 - min;
                min = 2;
                PreVisible = Visibility.Collapsed;
            }
            else
            {
                PreVisible = Visibility.Visible;
            }
            int max = Current + 2;
            if(max >= Pages - 1)
            {
                minAjust = max - Pages + 1;
                max = Pages - 1;
                NextVisible = Visibility.Collapsed;
            }
            else
            {
                NextVisible = Visibility.Visible;
            }
            min -= minAjust;
            min = min <= 2 ? 2 : min;
            max += maxAjust;
            max = max >= (Pages-1)?(Pages - 1):max;
            ObservableCollection<Pages> col = new ObservableCollection<Pages>();
            for(int i = min; i <= max; ++i)
            {
                Pages p = new controls.Pages();
                p.Name = i.ToString();
                p.PageIndex = i;
                col.Add(p);
            }
            this.PageCollection = col;
            if (Pages > 1)
            {
                LastVisible = Visibility.Visible;
            }
            else
            {
                LastVisible = Visibility.Collapsed;
            }
        }

        private void GotoItemPageClick(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            int page = (int)button.Tag;
            if (page >= 1 && page <= Pages)
            {
                Current = page;
                RaiseEvent(new RoutedEventArgs(LoadDataEvent, this));
            }
        }
    }
}
